package Qimo;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class ProductText {
    static ArrayList<Product> list=new ArrayList<>();
    static Scanner sc=new Scanner(System.in);
    static {
        list.add(new Product(1,"红楼梦",100.00,1));
        list.add(new Product(2,"三国演义",25.00,1));
        list.add(new Product(3,"西游记",38.00,0));
        list.add(new Product(4,"水浒传",66.00,1));
        list.add(new Product(5,"阿衰",89.00,1));
    }
    public static void main(String[] args) throws IOException {
        while (true){
            System.out.println("请输入编号：1.存入 2.读取 3.修改删除 4.同步");
            int n=sc.nextInt();
            switch (n){
                case 1:
                    asdf1();
                    break;
                case 2:
                    asdf2();
                    break;
                case 3:
                    asdf3();
                    break;
                case 4:
                    asdf4();
                    break;
            }
        }
    }

    private static void asdf4() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("product.txt"));
        for (Product product : list) {
            bw.write(product.toString());
            bw.newLine();
        }
        bw.close();
    }

    private static void asdf3() {
        for (Product product : list) {
            if (product.getFlag()==1){
                System.out.println(product.getPrice()*1.1);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getFlag()==0){
                list.remove(i);
            }
        }
    }

    private static void asdf2() throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("product.txt"));
        String str;
        while ((str=br.readLine())!=null){
        }
        for (Product product : list) {
            System.out.println(product);
        }
        br.close();
    }

    private static void asdf1() throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("product.txt"));
        for (Product product : list) {
            bw.write(product.toString());
            bw.newLine();
        }
        bw.close();
    }
}
